package cn.edu.scau.cmi.linpeiwei.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

public class MainController extends VBox implements Initializable {
	@FXML
	private AbstractFactoryController AbstractFactoryContent;
	@FXML
	private AdapterController AdapterContent;
	@FXML
	private ScrollPane sp;
	@FXML
	private TabPane tp;
	@FXML
	private TreeView tv;

	
	@FXML
	public void openTab(ActionEvent event) {
		System.out.println();
	}
	
	@FXML
	public void closeTab(ActionEvent event) {
		for(Tab tmp: tp.getTabs()) {
			if(tmp.isSelected()) {
				System.out.println(tmp.getId());
				tp.getTabs().remove(tmp);
				return;
			}
		}
	}
	
	@FXML
	public void quit(ActionEvent event) {
		System.exit(0);
	}
	
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		tp.prefWidthProperty().bind(sp.widthProperty());
		tp.prefHeightProperty().bind(sp.heightProperty());
		TreeItem<String> treeRoot = new TreeItem<>("软件体系结构实验");
		treeRoot.getChildren()
				.addAll(Arrays.asList(new TreeItem<String>("AbstractFactory"), new TreeItem<String>("Adapter"),
						new TreeItem<String>("Composite"), new TreeItem<String>("Comprehension"),
						new TreeItem<String>("Factory"), new TreeItem<String>("Multiton"),
						new TreeItem<String>("Singleton"), new TreeItem<String>("SimpleFactoryMethod")));

		// treeRoot.getChildren().get(2).getChildren().addAll(Arrays.asList(
		//
		// new TreeItem<String>("Child Node 4"),
		//
		// new TreeItem<String>("Child Node 5")
		// )
		// );

		tv.setShowRoot(true);

		tv.setRoot(treeRoot);
		tv.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {
				if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
					Node node = event.getPickResult().getIntersectedNode();

					if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
						String name = (String) ((TreeItem) tv.getSelectionModel().getSelectedItem()).getValue();
						Tab tmpTab = tabController(name);
						if(tmpTab != null) {
							if(!tabIsExist(tmpTab)) {
								tp.getTabs().add(tmpTab);
							}
							for(Tab tmpTab2: tp.getTabs()) {
								if(tmpTab2.getId().equals(tmpTab.getId())) {
									tp.getSelectionModel().select(tmpTab2);
								}
							}
							
						}
					}
				}
			}
		});
	}
	
	private Tab tabController(String name) {
		Tab tab = new Tab();
		Parent root;
		switch (name) {
		case "AbstractFactory":
			tab.setText("AbstractFactory");
			tab.setId("1");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/AbstractFactory.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}	
			break;
		case "Adapter":
			tab.setText("Adapter");
			tab.setId("2");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/Adapter.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			break;
		case "Composite":
			tab.setText("Composite");
			tab.setId("3");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/Composite.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			break;
		case "Comprehension":
			tab.setText("Comprehension");
			tab.setId("4");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/Comprehension.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			break;
		case "Factory":
			tab.setText("Factory");
			tab.setId("5");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/Factory.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			break;
		case "Multiton":
			tab.setText("Multiton");
			tab.setId("6");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/Multiton.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			break;
		case "Singleton":
			tab.setText("Singleton");
			tab.setId("7");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/Singleton.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			break;
		case "SimpleFactoryMethod":
			tab.setText("SimpleFactoryMethod");
			tab.setId("8");
			try {
				root = FXMLLoader.load(getClass().getResource("/cn/edu/scau/cmi/linpeiwei/view/SimpleFactoryMethod.fxml"));
				tab.setContent(root);
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			break;

		default:
			tab = null;
			break;
		}
		return tab;
	}
	
	private boolean tabIsExist(Tab tab) {
		for(Tab tmp: tp.getTabs()) {
			if(tmp.getId().equals(tab.getId())) {
				return true;
			}
		}
		return false;
	}
}
